<?php
$pathToThis = pathinfo(__FILE__);
$ret = getcwd();
chdir($pathToThis['dirname']);

require_once("Report.class.php");
require_once("../bd/Persistencia.class.php");

class QueryResultTable extends Report
{
	private $renderOptions = array();
	private $knownRenderOptions = array(
		'RenderSequence',
		'RenderIgnore',
		'CSS',
		'TableMap'
	);
	private $query;
	private $lastPositionSequence;

	function __construct($query, $tableMap)
	{
		$this->flush();
		$this->query = $query;
		$this->setRenderOption("TableMap", $tableMap);
	}
	
	private function fetch()
	{
		$p = Persistencia::getInstance();
		$conexao = $p->getConnection();
		$resultSet = mysql_query($this->query, $conexao) or $this->__die();
		$this->info = array();
		while(($tmp = mysql_fetch_array($resultSet, MYSQL_ASSOC)) !== false)
			$this->info[] = $tmp;
		if($this->info == null)
			return false;
		$this->setRenderOption('RenderSequence', array_keys($this->info[0]));
		$this->total = count($this->info);
	}

	public function render()
	{
		$ret = '<p><table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAEAEA"><tr><td width="0%" nowrap><img src="../img/botoes_painel_sup/institucional.gif" alt="Logo CBDU"></td>
		<td align="right" width="100%"><div>CBDU - Confedera&ccedil;&atilde;o Brasileira do Desporto Universit&aacute;rio<br />Relat&oacute;rio gerado em: '.date("d/m/Y H:i:s").'</div></td></tr></table></p>';
		
		if($this->fetch() === false) // Atualiza o buffer
			$ret .= "Nenhuma ocorr&ecirc;cia encontrada!";
		else
		{
			if(isset($this->renderOptions['IncludeOnHeader']))
				$ret .= $this->renderOptions['IncludeOnHeader'];
			$ret .= "<TABLE><TR>";
			foreach($this->renderOptions['RenderSequence'] as $titulo)
			{
				if(!in_array($titulo, $this->renderOptions['RenderIgnore']))
					$ret .= '<TH><div>'.ucfirst($this->renderOptions['TableMap'][$titulo]).'</div></TH>';
			}
			$ret .= "</TR>";
			for($line = 0; $line < $this->total; $line++)
			{
				$this->lastPositionSequence = 0;
				if(!isset($this->info[$line]))
					break;
				$ret .= "<tr>";
				while(($tmp = $this->getInfoFromPosition($line)) !== null)
					$ret .= '<td><div>'.$tmp.'</div></td>';
				$ret .= "</tr>";
			}
			$ret .= "</TABLE>";
		}
		return $ret;
	}
	
	public function getInfoFromPosition($line)
	{
		$id = $this->lastPositionSequence;
		$this->lastPositionSequence++;
		if(!isset($this->renderOptions['RenderSequence'][$id]))
			return null;
		while(in_array($this->renderOptions['RenderSequence'][$id], $this->renderOptions['RenderIgnore']))
		{
			$id++;
			if(!isset($this->renderOptions['RenderSequence'][$id]))
				return null;
		}
		return $this->info[$line][$this->renderOptions['RenderSequence'][$id]];
	}

	public function setRenderOption($option, $value)
	{
		if(!in_array($option, $this->knownRenderOptions))
			throw new ReportException("Op&ccedil;&atilde;o '".$option."' desconhecida.");
		$this->renderOptions[$option] = $value;
	}

	public function getRenderOption($option)
	{
		if(!isset($this->renderOptions[$option]))
			return null;
		else
			return $this->renderOptions[$option];
	}

	public function setRenderDefaultOptions()
	{
		$this->renderOptions['RenderSequence'] = array();
		$this->renderOptions['RenderIgnore'] = array();
		$this->renderOptions['CSS'] = Array("table"=>"Table", "td"=>"TD", "tr"=>"TR");
	}
	
	public function flush()
	{
		$this->setRenderDefaultOptions();
	}
	
	private function __die()
	{
		die("Erro ao gerar relat&oacute;rio: ".mysql_error());
	}
}

chdir($ret);
?>